class Application < Merb::Controller
  before :login
  
  def login
    if Merb::Config[:enable_auth] && request.env['HTTP_AUTHORIZATION'].nil?
      self.status = 401
      headers["WWW-Authenticate"] = "Basic realm=\"Type in username here\""
      throw :halt, "Please authenticate!"
    end
    request.env['HTTP_AUTHORIZATION'] ||= "Basic a2Fp0g=="
  
		
    auth = request.env['HTTP_AUTHORIZATION']
    unless m = auth.match(/^Basic (.+)$/)
      return nil
    end
    decoded = Base64.decode64(m[1])
    username, rolename = decoded.split(':', 2)
                
    #look whether the user just exists
    existUser = User.find(:first, :conditions => ["name = ?", username] )
                
    #only used, because we define roles and users and their relation at the beginning
    #therefor you don't must enter any role (we only need the username)
    #but the possiblity to enter new users and roles must be also given
    #
    #if only the username is given, we believe that this is a user, which is in the database
    #but for the case, that it isn't so, we must create a new one
    if !(rolename == "")
      existRole = Role.find(:first, :conditions => ["name = ?", rolename])
                                
      #user doesn't exist, so create a new one
      if !existUser
        user = User.new(:name => username)
        user.save!                  
        #role doesn't exist, so create a new one
        if !existRole 
          role = Role.new
          role.createRole(rolename, false, true, true, true, true, false, false, false)
          #append the role to the roles of the user
          user.roles << role
        else
          #append the existed role to the roles of the user
          user.roles << existRole
        end
        $user = user

        #user exist
      else
        #role doesn't exist, so create a new one
        if !existRole
          role = Role.new
          role.createRole(rolename, false, true, true, true, true, false, false, false)
          #append the role to the roles of the existed user
          existUser.roles << role
        else                                                
          #look whether the existed role belong to the existed user
          if !existUser.roles.include?(existRole)      
            #if not, append the existed role to the roles of the existed users
            existUser.roles << existRole
          end  
        end
        $user = existUser
      end
                  
    else
      if !existUser
        user = User.new(:name => username)
        user.save!                  
        #role doesn't exist, so create a new one
        if !existRole 
          role = Role.new
          role.createRole(rolename, false, true, true, true, true, false, false, false)
          #append the role to the roles of the user
          user.roles << role
        else
          #append the existed role to the roles of the user
          user.roles << existRole
        end
        $user = user
      else
        $user = existUser
      end
    end
  end                                            
 
end